
//  HMEmoticonTools.swift
//  微博
//
//  Created by 程双岐 on 16/10/6.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit

let NumberOfPage = 20
let recentDataPath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as NSString) .appendingPathComponent("recenEmoticonArray.archiver")

class HMEmoticonTools: NSObject {
    static let shareTools: HMEmoticonTools = HMEmoticonTools()
    
    private override init(){
        super.init()
        
    }
    
    lazy var emoticonBundle: Bundle = {
        let path = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil)!
        let bundle = Bundle(path: path)!
        return bundle
    }()
    
    //给表情试图提供一个三维数组
    lazy var allEmoticonsArray: [[[HMEmoticon]]] = {
        
        
        return [
            
            [self.recenEmoticonArray],
            self.sectionEmoticon(emoticonArray: self.defaultEmoticonsArray),
            self.sectionEmoticon(emoticonArray: self.emojiEmoticonsArray),
            self.sectionEmoticon(emoticonArray: self.lxhEmoticonsArray),
            
            ]
        
    }()
    
    private lazy var recenEmoticonArray: [HMEmoticon] = {
        if let localRecentData = self.loadRecentEmoticonArray(){
            //本地有最近表情数据 直接返回解可以
            return localRecentData
        }else{
        
        let emoticonArray = [HMEmoticon]()
        return emoticonArray
        }
    }()
    
    private lazy var defaultEmoticonsArray: [HMEmoticon] = {
        
        return self.loadEmoticonArray(folderName: "default", fileName: "info.plist")
        
    }()
    private lazy var emojiEmoticonsArray: [HMEmoticon] = {
        
        return self.loadEmoticonArray(folderName: "emoji", fileName: "info.plist")
        
    }()
    private lazy var lxhEmoticonsArray: [HMEmoticon] = {
        
        return self.loadEmoticonArray(folderName: "lxh", fileName: "info.plist")
        
    }()
    
    private func loadEmoticonArray(folderName: String, fileName: String) -> [HMEmoticon]{
        
        let sunPath = folderName + "/" + fileName
        
        //self.emoticonBundle.path(forResource: xxx) 可以透过两层文件夹
        let path = self.emoticonBundle.path(forResource: sunPath, ofType: nil)!
        
        let dictArray = NSArray(contentsOfFile: path) as! [[String: Any]]
        
        let modelArray = NSArray.yy_modelArray(with: HMEmoticon.self, json: dictArray) as! [HMEmoticon]
        //遍历数组模型 如果是图片类型 需要拼接路径
        for model in modelArray{
            if model.type == "0"{
                let imagePath = self.emoticonBundle.path(forResource: folderName, ofType: nil)! + "/" + model.png!
                model.path = imagePath
                model.folderName = folderName
            }
            
        }
        return modelArray

    }
        //  通过表情数组拆分成二维数组
        private func sectionEmoticon(emoticonArray: [HMEmoticon]) -> [[HMEmoticon]] {
            
            //   根据数组个数计算页数
            let pageCount = (emoticonArray.count - 1) / NumberOfPage + 1
            
            var tempArray = [[HMEmoticon]]()
            
            //  遍历页数,截取相应数据
            for i in 0..<pageCount {
                
                //  开始截取的索引
                let loc = i * NumberOfPage
                //  截取的长度
                var len = NumberOfPage
                //  表示数组越界
                if loc + len > emoticonArray.count {
                    //  获取截取剩余个数
                    len = emoticonArray.count - loc
                }
                
                
                let subArray = (emoticonArray as NSArray).subarray(with: NSMakeRange(loc, len)) as! [HMEmoticon]
                
                tempArray.append(subArray)
            }
            return tempArray
            
            
        }
    func saveRecentEmoticon(emoticon: HMEmoticon) {
        
        //        recentEmoticonArray.append(emoticon)
        //  1. 添加表情模型之前判断最近表情这组数据里面是否已经存在对应的表情, 如果存在删除指定表情
        for (i,etn) in recenEmoticonArray.enumerated(){
            if emoticon.type == "0" {
                if etn.chs == emoticon.chs {
                    //在最近表情数组里面找打了这个表情
                    recenEmoticonArray.remove(at: i)
                    break
                }
                
            }else{
                if etn.code == emoticon.code {
                    recenEmoticonArray.remove(at: i)
                    break
                }
                
            }
            
        }
        
        //  2. 点击表情添加到最近表情数组的第一个元素
        
        recenEmoticonArray.insert(emoticon, at: 0)
        
        //  3. 如果超过20个表情,删除最近表情里面的最后一个元素
        while recenEmoticonArray.count > 20 {
            recenEmoticonArray.removeLast()
        }
        
        //  4. 更新数据源 (allEmoticonsArray)
        
        allEmoticonsArray[0][0] = recenEmoticonArray
        
        //  5. 把最近表情进行归档
        

        print(recentDataPath)
        NSKeyedArchiver.archiveRootObject(recenEmoticonArray, toFile: recentDataPath)
        
    }
    
    private func loadRecentEmoticonArray() -> [HMEmoticon]? {
        
       return NSKeyedUnarchiver.unarchiveObject(withFile: recentDataPath) as? [HMEmoticon]
    }
    
    //根据表情描述查找默认表情数组和浪小花表情数组里的元素
    
    func selectedEmoticonarray(chs: String) -> HMEmoticon? {
        
        //默认表情里面去找
        for emoticon in defaultEmoticonsArray{
            if emoticon.chs == chs {
                return emoticon
            }
        }
        for emoticon in lxhEmoticonsArray{
            if emoticon.chs == chs {
                return emoticon
            }
        }
        
        return nil
    }
    
        
}
